var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
app.getCommonTime();
Page({
    data: {
        showDialog:false,
        detailData:{},
        commonUrl:app.globalData.commonUrl,
        commontListData:[],
        commentCreateTime:[],
        commentText:'',
        scrollPageNum:1,
        orderTotalPage:'',
        onLoading:false,
        noMoreData:false,
        bindDownLoad:false,
        winHeight:"",//窗口高度
    },
    intoMap:function(){
  
        var that=this;
        var addInfo=that.data.detailData;
        wx.openLocation({//​使用微信内置地图查看位置。
            latitude: Number(addInfo.latitude),
            longitude: Number(addInfo.longitude),
            name: addInfo.proName,
            address:addInfo.proAddress
        })
    },
    
    goEvaluate:function(){
     
        wx.navigateTo({
            url: '../evaluate/evaluate'
        })
    },
    changeInValue:function(e){
        console.log(e.detail.value);
    },
    //评论提交
    commentBtn:function(e){
        var cont=e.detail.value;
        if(cont!=''){
            this.subComment(cont);
            this.setData({
                commentText:''
            });
        }else{
            wx.showModal({
                icon:'error',
                content: '评论不能输入为空',
                success:function(res){
                    if(res.confirm){

                    }else if(res.cancel){

                    }
                },
            })
        }
    },
  EventHandle:function(){
    var videoContext = wx.createVideoContext('myvideo', this);
    videoContext.requestFullScreen();//执行全屏方法
   
  },
    showEvaluateDetail:function(){
        this.setData({
            showDialog:!this.data.showDialog
        });
        let proId=this.data.detailData.id;
        if(this.data.showDialog&&proId){
            this.getCommontList(proId);
        }
    },
    subComment:function(content){
        var params={
            userToken:app.globalData.openid,
            proId:this.data.detailData.id,
            content:content
        };
        var that=this;
        app.handlerPost('/api/project/subAppraises.do',params,"GET").then( (res)=>{
            if(res.data.code=="SUCCESS"){
                wx.showToast({
                    title: '登录成功',
                    icon: 'success',
                    duration: 2000,
                });
                that.getCommontList(this.data.detailData.id);
                that.getProjectDetailData(this.data.detailData.id)
           }
            app.fetchAddressList();
        }).catch( (errMsg)=>{
            console.log(errMsg);
        } );
    },
    bindDownLoad:function(e){
    
        let that=this;
        this.setData({
            bindDownLoad:true
        });
        if(this.data.orderTotalPage==this.data.scrollPageNum){
            this.setData({
                noMoreData:true,
                onLoading:false,
            });
            return;
        }else{
            this.data.scrollPageNum++;
            this.setData({
                onLoading:true,
                scrollPageNum:this.data.scrollPageNum
            });
        }
        that.getCommontList(this.data.detailData.id);

    },
  play:function(){
    var videoContext = wx.createVideoContext('detail-video', this);
    videoContext.requestFullScreen();//执行全屏方法s
  },
    refresh:function(e){
        wx.showNavigationBarLoading();
        let that=this;
        that.setData({
            scrollPageNum:1,
            bindDownLoad:false
        });
        that.getCommontList(this.data.detailData.id);
    },
    getCommontList:function(proId){
        var that=this;
        wx.showLoading({
            title: '加载中',
        });
        app.handlerPost('/api/project/pageAppraises.do',{'page.page':this.data.scrollPageNum,userToken:app.globalData.openid,proId:proId},"GET").then( (res)=>{
            if(res.data.code=="SUCCESS"){
                var data=res.data.data.items;
                that.setData({
                    orderTotalPage:res.data.data.totalPage
                });
                var commentCreateTime=data.map(function(value,key){
                    var obj={
                        createTime:new Date(value.createTime).Format("yyyy-MM-dd")
                    }
                    return obj;
                });
                if(that.data.scrollPageNum==1){
                    that.setData({
                        commontListData:data,
                        commentCreateTime:commentCreateTime
                    });
                }else{
                    that.setData({
                        commontListData:that.data.commontListData.concat(data),
                        commentCreateTime:that.data.commentCreateTime.concat(commentCreateTime)
                    });
                }

            }
            setTimeout(function() {
            
                wx.hideNavigationBarLoading();
                wx.stopPullDownRefresh();
            },1000);
            app.fetchAddressList();
        }).catch( (errMsg)=>{
            console.log(errMsg);
        } );
    },
    onShow:function(){
   
        var that = this;
        //  高度自适应
        wx.getSystemInfo( {
            success: function( res ) {
                var clientHeight=res.windowHeight,
                    clientWidth=res.windowWidth,
                    rpxR=750/clientWidth;
                var  calc=clientHeight*rpxR+200;
                that.setData( {
                    winHeight: calc
                });
            }
        });
    },
    getProjectDetailData:function(proId){
  
        var that=this;
        wx.showLoading({
            title: '加载中',
        });
        app.handlerPost('/api/project/projectDetail.do',{userToken:app.globalData.openid,proId:proId},"GET").then( (res)=>{
            if(res.data.code=="SUCCESS"){
                that.setData({
                    detailData:res.data.data
                });
                // var article= that.data.detailData.context; 1
                // WxParse.wxParse('article', 'html', article,that, 5);
            }
            app.fetchAddressList();
        }).catch( (errMsg)=>{
            console.log(errMsg);
        } );
    },
    onLoad: function (options) {
  
        var that=this;
        if(app.globalData.openid==''||app.globalData.openid==undefined){
            wx.login({
                success:function(res){
                    app.login({code:res.code});
                }
            });
        }
        that.getProjectDetailData(options.proId);
        that.getCommontList(options.proId);
    },
    onPageScroll:function(e){
    
    },
    onShareAppMessage: function (res) {
    
        if (res.from === 'button') {
            console.log(res.target)
        }
        return {
            title: 'GOODPLACE',
          path: '/pages/goodPlaceDetail/goodPlaceDetail?openid=' + app.globalData.openid + '&proId=' + this.data.detailData.id,
            success: function (res) {
                console.log(res);
            },
            fail: function (res) {
                console.log(res);
            }
        }
    },
});
